function loginMpWeixin() {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success(res) {
				uni.request({
					url: 'https://97fca9f2-41f6-449f-a35e-3f135d4c3875.bspapp.com/http/user-center',
					method: 'POST',
					data: {
						action: 'loginByWeixin',
						params: {
							code: res.code,
							platform: 'mp-weixin'
						}
					},
					success(res) {
						if (res.data.code !== 0) {
							reject(new Error('获取openid失败：', res.result.msg))
							return
						}
						uni.setStorageSync('openid', res.data.openid)
						resolve(res.data.openid)
					},
					fail(err) {
						reject(new Error('获取openid失败：' + err))
					}
				})
			},
			fail(err) {
				reject(err)
			}
		})
	})
}
export default async function weixinPay() {
	let openid = uni.getStorageSync('openid')
	console.log("发起支付");
	this.loading = true;
	if (!openid) {
		try {
			openid = await this.loginMpWeixin()
		} catch (e) {
			console.error(e)
		}

		if (!openid) {
			uni.showModal({
				content: '获取openid失败',
				showCancel: false
			})
			this.loading = false
			return
		}
	}
	this.openid = openid
	let orderInfo = await this.getOrderInfo('wxpay')
	if (!orderInfo) {
		uni.showModal({
			content: '获取支付信息失败',
			showCancel: false
		})
		return
	}
	uni.requestPayment({
		...orderInfo,
		success: (res) => {
			uni.showToast({
				title: "感谢您的赞助!"
			})
		},
		fail: (res) => {
			uni.showModal({
				content: "支付失败,原因为: " + res
					.errMsg,
				showCancel: false
			})
		},
		complete: () => {
			this.loading = false;
		}
	})
}
async function requestPayment(e, index) {
	this.providerList[index].loading = true;
	const provider = e.id
	let orderInfo = await this.getOrderInfo(provider);
	if (!orderInfo) {
		uni.showModal({
			content: '获取支付信息失败',
			showCancel: false
		})
		return
	}
	console.log('--------orderInfo--------')
	console.log(orderInfo);
	uni.requestPayment({
		provider,
		orderInfo: orderInfo,
		success: (e) => {
			console.log("success", e);
			uni.showToast({
				title: "感谢您的赞助!"
			})
		},
		fail: (e) => {
			console.log("fail", e);
			uni.showModal({
				content: "支付失败,原因为: " + e.errMsg,
				showCancel: false
			})
		},
		complete: () => {
			this.providerList[index].loading = false;
		}
	})
},
function getOrderInfo(provider) {
	return new Promise((resolve, reject) => {
		if (!this.price) {
			reject(new Error('请输入金额'))
		}
		uni.request({
			method: 'POST',
			url: 'https://97fca9f2-41f6-449f-a35e-3f135d4c3875.bspapp.com/http/pay',
			data: {
				provider,
				openid: this.openid,
				totalFee: Number(this.price) * 100, // 转为以分为单位
				// #ifdef APP-PLUS
				platform: 'app-plus',
				// #endif
				// #ifdef MP-WEIXIN
				platform: 'mp-weixin',
				// #endif
			},
			success(res) {
				if (res.data.code === 0) {
					resolve(res.data.orderInfo)
				} else {
					reject(new Error('获取支付信息失败' + res.data.msg))
				}
			},
			fail(err) {
				reject(new Error('请求支付接口失败' + err))
			}
		})
	})
}